package com.yunhai.xc.lina.base;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler extends RuntimeException {
	private static final long serialVersionUID = 1L;
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@ExceptionHandler(value = Exception.class)
	public Object catchException(Exception e, HttpServletRequest request) {
		logger.error("Exception:{}", e);
		if (e instanceof UnauthenticatedException) {
			return "index";
		} else if (e instanceof UnauthorizedException) {
			return "shiro/403";
		} else if (e instanceof IncorrectCredentialsException) {
			request.setAttribute("msg", "用户名或者密码错误");
			return "index";
		}
		return null;
	}
}
